// 导入数据库
const db = require('../db/index')
const fs = require('node:fs')

// 上传合同文件 files
exports.uploadFile = (req, res) => {
    const oldName = req.files[0].filename
    const newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8')
    const sql1 = "select * from file where file_name = ?"
    db.query(sql1, newName, (err, result) => {
        if (err) return res.cc(err)
        if (result.length > 0) {
            // 使用fs的unlink对文件进行删除
            fs.unlink(`./public/upload/${oldName}`, (err, result) => { if (err) return res.cc(err) })
            return res.send({ status: 1, message: '文件名已存在' })
        }
        // 在本地存储文件并修改文件名
        fs.renameSync('./public/upload/' + oldName, './public/upload/' + newName)
        // 数据库中插入文件地址file_url
        const sql2 = 'insert into file set ?'
        db.query(sql2, {
            file_name: newName,
            file_url: `http://127.0.0.1:3007/upload/${newName}`,
            file_size: req.files[0].size * 1 / 1024,
            upload_time: new Date(),
            download_number: 0
        }, (err, result) => {
            if (err) return res.cc(err)
            res.send({
                status: 0,
                message: '上传文件成功',
                url: `http://127.0.0.1:3007/upload/${newName}`
            })
        })
    })
}

// 绑定上传人和文件 upload_person,file_url =>
exports.bindUploadPerson = (req, res) => {
    const { upload_person, file_url } = req.body
    const sql1 = "update file set upload_person = ? where file_url = ?"
    db.query(sql1, [upload_person, file_url], (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '绑定上传人和文件成功' })
    })
}

// 更新下载量 id =>
exports.updateDownloadNumber = (req, res) => {
    const { id } = req.body
    db.query("select * from file where id = ?", id, (err, result) => {
        if (err) return res.cc(err)
        const sql1 = "update file set download_number = ? where id = ?"
        db.query(sql1, [result[0].download_number + 1, id], (err, result) => {
            if (err) return res.cc(err)
            res.send({ status: 0, message: '更新下载量成功' })
        })
    })
}

// 文件名模糊搜索文件 file_name =>
exports.searchFileByName = (req, res) => {
    const { file_name } = req.body
    const sql1 = `select * from file where file_name like '%${file_name}%'`
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 删除文件 id,file_name =>
exports.deleteFile = (req, res) => {
    const { id, file_name } = req.body
    const sql1 = "delete from file where id = ?"
    db.query(sql1, id, (err, result) => {
        if (err) return res.cc(err)
        // 使用fs的unlink对文件进行删除
        fs.unlink(`./public/upload/${file_name}`, (err, result) => { if (err) return res.cc(err) })
        res.send({ status: 0, message: '删除文件成功' })
    })
}

// 分页管理：====================================
// 获取文件数量 无 =>
exports.getFileNumber = (req, res) => {
    const sql1 = "select * from file"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}
// 文件列表监听换页 页码pager =>
exports.returnFileData = (req, res) => {
    // 传来的页码
    const { pager } = req.body
    const number = (pager - 1) * 10
    // 一页10个数据
    // limit 10 offset ${number} 表示从第number个起取10个
    const sql1 = `select * from file order by upload_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) {
            return res.cc(err)
        }
        res.send(result)
    })
}